package view.GUI.otherView;

import model.Configuration;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.controlsfx.dialog.Dialogs;
import org.controlsfx.validation.ValidationSupport;

import enums.GUIText;
import enums.Message;
import view.GUI.mainView.MainView;
import view.Validator.MyValidationSupport;
import view.Validator.PasswordValidator;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

public final class ChangePWView extends Application {

	private final static Logger log = LogManager.getLogger("ERROR");

	private Label lblOldPw;
	private Label lblNewPw;
	private Label lblConfirm;

	private TextField txtOldPW;
	private TextField txtNewPw;
	private TextField txtConfirm;

	private Button btnOk;
	private Button btnCancel;

	private MainView mainView;
	private Stage primaryStage;

	private GridPane grid;

	private ValidationSupport valSup;
	
	private Separator sepTop;
	private Separator sepBottom;
	private Label lblTitle;

	public ChangePWView(MainView mainView) {
		this.mainView = mainView;
		createWidgets();

		
		
		setupLayout();

		addWidgets();

		setupInteraction();
		try {
			start(new Stage());
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
	}

	private void setupLayout() {
		GridPane.setColumnSpan(sepBottom, 4);
		GridPane.setColumnSpan(sepTop, 4);
		grid.getStyleClass().add("grid");
		lblTitle.setId("topic");
	}

	private void createWidgets() {
		valSup = MyValidationSupport.createDefaultValidationSupport();
		this.grid = new GridPane();
		lblOldPw = new Label(GUIText.OLDPW.toString());
		lblNewPw = new Label(GUIText.NEWPW.toString());
		lblConfirm = new Label(GUIText.VALIDATENEWPW.toString());
		txtOldPW = new PasswordField();
		txtNewPw = new PasswordField();
		txtConfirm = new PasswordField();
		btnOk = new Button(GUIText.OK.toString());
		btnCancel = new Button(GUIText.CANCEL.toString());
		sepTop = new Separator();
		sepBottom = new Separator();
		lblTitle = new Label(GUIText.CHANGEPW.toString());
		
		setupValidation();
	}

	private void setupValidation() {
		valSup.registerValidator(txtConfirm, true, new PasswordValidator());
		valSup.registerValidator(txtNewPw, true, new PasswordValidator());
		valSup.registerValidator(txtOldPW, true, new PasswordValidator());
	}

	private void addWidgets() {
		int rowCounter = 0;
		grid.add(lblTitle, 0, rowCounter++);
		grid.add(sepTop, 0, rowCounter++);
		grid.add(lblOldPw, 0, rowCounter);
		grid.add(txtOldPW, 1, rowCounter++);

		grid.add(lblNewPw, 0, rowCounter);
		grid.add(txtNewPw, 1, rowCounter++);

		grid.add(lblConfirm, 0, rowCounter);
		grid.add(txtConfirm, 1, rowCounter++);

		grid.add(btnOk, 0, rowCounter);
		grid.add(btnCancel, 1, rowCounter++);
	}

	private void setupInteraction() {

		txtConfirm.setOnKeyReleased(new EventHandler<KeyEvent>() {

			@Override
			public void handle(KeyEvent arg0) {

				btnOk.setDisable(MyValidationSupport.isNodeToDisable(valSup));

			}
		});
		txtOldPW.setOnKeyReleased(new EventHandler<KeyEvent>() {

			@Override
			public void handle(KeyEvent arg0) {

				btnOk.setDisable(MyValidationSupport.isNodeToDisable(valSup));
			}
		});

		txtNewPw.setOnKeyReleased(new EventHandler<KeyEvent>() {

			@Override
			public void handle(KeyEvent arg0) {

				btnOk.setDisable(MyValidationSupport.isNodeToDisable(valSup));
			}
		});

		btnOk.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent e) {
				if (!mainView.checkIfOldPWEquals(txtOldPW.textProperty().getValue())) {
					Dialogs.create().message(Message.ENTEREDOLDPWWRONG.toString()).showError();
				} else if (!txtNewPw.textProperty().getValue().equals(txtConfirm.textProperty().getValue())) {
					Dialogs.create().message(Message.PWVERIFICATIONWRONG.toString()).showError();
				} else {
					mainView.setNewPassword(mainView.getWorkingStaff(), txtNewPw.textProperty().getValue());
					primaryStage.close();
				}
			}
		});

		btnCancel.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent arg0) {

				primaryStage.close();
			}
		});
	}

	@Override
	public void start(Stage primaryStage) throws Exception {

		this.primaryStage = primaryStage;
		Scene scene = new Scene(grid);
		scene.getStylesheets().add(getClass().getResource(Configuration.STYLECHANGEPW).toExternalForm());
		scene.getStylesheets().add(getClass().getResource(Configuration.STYLEROOT).toExternalForm());
		this.primaryStage.initModality(Modality.WINDOW_MODAL);
		primaryStage.initOwner(mainView.getPrimaryStage());
		primaryStage.setScene(scene);
		primaryStage.setTitle(GUIText.CHANGEPW.toString());
		primaryStage.centerOnScreen();
		primaryStage.show();
	}
}
